﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PictureResultScript : MonoBehaviour {

    public Transform ResultPictureParent;
    public List<Image> ImageTileList = new List<Image>();

    public Button ResultBtn;
    public GameObject MessageSend;
    public List<GameObject> AnswerList;
    public GameObject MissPictureObject;

    public const int RESULT_WIDTH = 8;
    public const int RESULT_HEIGHT = 8;
    private const int CELL_SIZE = 45;
    private static string PicturePath = "Pictures/Result/";
    private const float pictureShowInterval = 0.02f;
    private const float messageShowInterval = 0.3f;

    private bool isFailed;

	// Use this for initialization
	void Start () {
        ResultBtn.onClick.AddListener(TryBack);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    [ContextMenu("test aaa")]
    public void ShowResultTest()
    {
        ShowResult(2, 2, 2, 2);
    }

    public void ShowResult()
    {
        //算分数
        ClearAll();
        isFailed = GameManager.GetInstance().IsFailed();
        ShowResult(GameManager.GetInstance().GetMissCount(), GameManager.GetInstance().GetMessCount(), GameManager.GetInstance().GetChaosCount(), GameManager.GetInstance().GetExtraCount());
        StartCoroutine(ShowResultPicSlowly());
    }

    public void ShowResult(int missCount,int messCount,int chaosCount, int extraCount)
    {
        //clear

        foreach (Image image in ImageTileList)
        {
            image.sprite = null;
        }

        //Sprite[] sprites = Resources.LoadAll<Sprite>(PicturePath + PictureLogicScript.ImageIndex);
        Sprite[] sprites = PictureLogicScript.sGetInstance().GetCorrectSprite();

        int resultCount = RESULT_HEIGHT * RESULT_WIDTH;
        List<int> idList = new List<int>();
        List<int> resultList = new List<int>();
        for (int i = 0; i < resultCount; i++)
        {
            resultList.Add(i);
            idList.Add(i);
        }
        if (missCount > 0)
        {
            if (missCount > resultCount)
            {
                missCount = resultCount;
            }
            List<int> tempList = new List<int>(idList);
            for (int i = 0; i < missCount; i++)
            {
                int missId = tempList[Random.Range(0, tempList.Count)];
                resultList[missId] = -1;
                tempList.Remove(missId);
            }
        }
        if (messCount > 0)
        {
            if (missCount > resultCount)
            {
                missCount = resultCount;
            }
            List<int> tempList = new List<int>(idList);
            for (int i = 0; i < missCount; i++)
            {
                int missId = tempList[Random.Range(0, tempList.Count)];
                int missTwoId = tempList[Random.Range(0, tempList.Count)];
                int temp = resultList[missId];
                resultList[missId] = resultList[missTwoId];
                resultList[missTwoId] = temp;
            }
        }
        List<int> chaosList = new List<int>();
        if (chaosCount > 0)
        {
            if (chaosCount > resultCount)
            {
                chaosCount = resultCount;
            }
            List<int> tempList = new List<int>(idList);
            for (int i = 0; i < chaosCount; i++)
            {
                int chaosId = tempList[Random.Range(0, tempList.Count)];
                chaosList.Add(chaosId);
                tempList.Remove(chaosId);
            }
        }
        List<int> extraList = new List<int>();
        if (extraCount > 0)
        {
            if (extraCount > resultCount)
            {
                extraCount = resultCount;
            }
            List<int> tempList = new List<int>(idList);
            for (int i = 0; i < extraCount; i++)
            {
                int extraId = tempList[Random.Range(0, tempList.Count)];
                extraList.Add(extraId);
                tempList.Remove(extraId);
            }
        }

        //show
        for (int i = 0; i < resultCount; i++)
        {
            if (ImageTileList.Count > i)
            {
                if (resultList[i] >= 0)
                {
                    int result = resultList[i];
                    ImageTileList[i].sprite = sprites[result];
                    ImageTileList[i].transform.rotation = Quaternion.identity;
                    if (chaosList.Contains(i))
                    {
                        ImageTileList[i].transform.Rotate(new Vector3(0, 180, 0));
                    }
                    if (extraList.Contains(i))
                    {
                            ImageTileList[i].sprite = PictureLogicScript.sGetInstance().GetErrorSprite();
                    }
                }
            }
        }
    }

    [ContextMenu("aaa")]
    public void CreateGrid()
    {
        if (ResultPictureParent != null)
        {
            ImageTileList.Clear();
            for(int i = 0;i < RESULT_HEIGHT;i++)
            {
                for (int j = 0; j < RESULT_WIDTH; j++)
                {
                    GameObject gameobject = new GameObject();
                    gameobject.transform.parent = ResultPictureParent;
                    gameobject.transform.localPosition = new Vector3(CELL_SIZE * j, -CELL_SIZE * i, 0);
                    gameobject.name = "img" + (i * RESULT_WIDTH + j);
                    Image imageScript = gameobject.AddComponent<Image>();

                    gameobject.GetComponent<RectTransform>().sizeDelta = new Vector2(CELL_SIZE, CELL_SIZE);
                    ImageTileList.Add(imageScript);
                }
            }
        }
    }

    public void TryBack()
    {
        GameApp.GetInstance().TryBackToChoose();
    }

    public void ClearAll()
    {
        int count = ImageTileList.Count;
        for (int i = 0; i < count; i++)
        {
            ImageTileList[i].gameObject.SetActive(false);
        }
        MissPictureObject.SetActive(false);
    }

    private IEnumerator ShowResultPicSlowly()
    {
        yield return new WaitForSeconds(messageShowInterval);
        MessageSend.SetActive(true);
        yield return new WaitForSeconds(messageShowInterval);
        if (isFailed)
        {
            MissPictureObject.SetActive(true);
        }
        else
        {
            int count = ImageTileList.Count;
            for (int i = 0; i < count; i++)
            {
                ImageTileList[i].gameObject.SetActive(true);
                yield return new WaitForSeconds(pictureShowInterval);
            }
        }
        yield return new WaitForSeconds(messageShowInterval);
        AnswerList[0].SetActive(true);
    }
}
